package com.aitou.mrylive.presentation.ui.login;

import android.content.Context;
import android.util.Log;

import com.aitou.mrylive.data.bean.BaseResponse;
import com.aitou.mrylive.data.bean.LoginInfo;
import com.aitou.mrylive.domain.LocalDataManager;
import com.aitou.mrylive.domain.LoginManager;
import com.aitou.mrylive.presentation.ui.base.BaseObserver;
import com.aitou.mrylive.presentation.ui.base.BasePresenter;
import com.umeng.analytics.MobclickAgent;

import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author Muyangmin
 * @since 1.0.0
 */
public class LoginPresenter extends BasePresenter<LoginUiInterface> {

    private LoginManager loginManager;
    private Context context;
    public LoginPresenter(LoginUiInterface uiInterface) {
        super(uiInterface);
        loginManager = new LoginManager();
        this.context= (Context) uiInterface;
    }

    public void login(String name, String type){
        Subscription subscription = loginManager.login(name, type)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<LoginInfo>>(getUiInterface()) {
                    @Override
                    public void onSuccess(BaseResponse<LoginInfo> response) {
                        LocalDataManager.getInstance().saveLoginInfo(response.getData());
                        getUiInterface().startActivityAndFinishOthers();
                    }
                });
        addSubscription(subscription);
    }

    public void loginByCaptcha(final String phone, String captcha){
        Subscription subscription = loginManager.loginByCaptcha(phone, captcha)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<LoginInfo>>(getUiInterface()) {
                    @Override
                    public void onSuccess(BaseResponse<LoginInfo> response) {
//                        MobclickAgent.onEvent(context,"phone",phone);
                        LocalDataManager.getInstance().saveLoginInfo(response.getData());
                        getUiInterface().startActivityAndFinishOthers();
                    }
                });
        addSubscription(subscription);
    }

    public void sendCaptcha(String phoneNum){
        Subscription subscription = loginManager.sendCaptcha(phoneNum)
                .compose(this.<BaseResponse<String>>applyAsySchedulers())
                .subscribe(new BaseObserver<BaseResponse<String>>(getUiInterface()) {
                    @Override
                    public void onSuccess(BaseResponse<String> response) {
                        //Empty
                    }
                });
        addSubscription(subscription);
    }
}
